package com.cefa.finance.controller;

import com.cefa.finance.model.AccidentRecord;
import com.cefa.finance.model.Contract;
import com.cefa.finance.model.MaintenanceRecord;
import com.cefa.finance.result.ResultModel;
import com.cefa.finance.service.VehicleInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("vehicleInfo")
public class VehicleInfoController {

    private static final Logger logger = LoggerFactory.getLogger(VehicleInfoController.class);

    @Autowired
    private VehicleInfoService vehicleInfoService;

    @RequestMapping(path = "addMainRecord", method = RequestMethod.POST)
    @CrossOrigin
    public ResultModel addMaintenanceRecord(MaintenanceRecord maintenanceRecord) {
        try {
            return vehicleInfoService.addMaintenanceRecord(maintenanceRecord);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultModel.ERROR(e.getMessage());
        }
    }

    @RequestMapping(path = "updateMainRecord", method = RequestMethod.POST)
    @CrossOrigin
    public ResultModel updateMaintenanceRecord(MaintenanceRecord maintenanceRecord) {
        try {
            return vehicleInfoService.addMaintenanceRecord(maintenanceRecord);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultModel.ERROR(e.getMessage());
        }
    }

    @RequestMapping(path = "deleteMainRecord", method = RequestMethod.POST)
    @CrossOrigin
    public ResultModel deleteMaintenanceRecord(MaintenanceRecord maintenanceRecord) {
        try {
            return vehicleInfoService.deleteMaintenanceRecord(maintenanceRecord);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultModel.ERROR(e.getMessage());
        }
    }


    @RequestMapping(path = "addAccidentRecord", method = RequestMethod.POST)
    @CrossOrigin
    public ResultModel addAccidentRecord(AccidentRecord accidentRecord) {
        try {
            return vehicleInfoService.addAccidentRecord(accidentRecord);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultModel.ERROR(e.getMessage());
        }
    }

    @RequestMapping(path = "updateAccidentRecord", method = RequestMethod.POST)
    @CrossOrigin
    public ResultModel updateAccidentRecord(AccidentRecord accidentRecord) {
        try {
            return vehicleInfoService.addAccidentRecord(accidentRecord);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultModel.ERROR(e.getMessage());
        }
    }

    @RequestMapping(path = "deleteAccidentRecord", method = RequestMethod.POST)
    @CrossOrigin
    public ResultModel deleteMaintenanceRecord(AccidentRecord accidentRecord) {
        try {
            return vehicleInfoService.deleteaccidentRecord(accidentRecord);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultModel.ERROR(e.getMessage());
        }
    }

    @RequestMapping(path = "updatetravlledKms", method = RequestMethod.POST)
    @CrossOrigin
    public ResultModel updatetravlledKms(Contract contract) {
        try {
            return vehicleInfoService.updatetravlledKms(contract);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultModel.ERROR(e.getMessage());
        }
    }

    @RequestMapping(path = "listVehicleInfo", method = RequestMethod.POST)
    @CrossOrigin
    public ResultModel listVehicleInfo(String searchText, Integer pageNo, Integer pageCount) {
        try {
            return vehicleInfoService.listVehicleInfo(searchText, pageNo, pageCount);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultModel.ERROR(e.getMessage());
        }
    }
}
